﻿#region MIT
/*
Engine Of Evermore (http://code.google.com/p/engineofevermore/)
Copyright (c) 2008, 2009 Michael Woerister

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EngineOfEvermore.Editor.Common.Command;
using EngineOfEvermore.Scene;
using System.Diagnostics;
using EngineOfEvermore.Common;

namespace EngineOfEvermore.Editor.SceneEditor
{
    class ObjectLibraryDropCommand : ICommand
    {
        public SceneModel SceneModel { get; private set; }
        public SceneObject SceneObject { get; private set; }
        public String ResourceName { get; private set; }
        public Vector2d DropPosition { get; private set; }

        private IEnumerable<SceneObject> _previousSelection;

        public ObjectLibraryDropCommand( SceneModel model, SceneObject sceneObject, 
            String resourceName, Vector2d dropPosition )
        {
            Debug.Assert( model != null );
            Debug.Assert( sceneObject != null );
            Debug.Assert( resourceName != null );
            
            this.SceneModel = model;
            this.SceneObject = sceneObject;
            this.ResourceName = resourceName;
            this.DropPosition = dropPosition;
        }

        public string Description
        {
            get { return "Add \"" + this.ResourceName + "\" from ObjectLibrary"; }
        }

        public void Execute()
        {
            Debug.Assert( _previousSelection == null );
            Debug.Assert( this.SceneObject.SceneGraph == null );

            _previousSelection = this.SceneModel.Selection.ToArray();

            this.SceneObject.Position = this.DropPosition;

            this.SceneModel.SceneGraph.AddSceneObject( this.SceneObject );
            this.SceneModel.Selection.Set( this.SceneObject );
        }

        public void Undo()
        {
            this.SceneModel.Selection.Set( _previousSelection );
            this.SceneModel.SceneGraph.RemoveSceneObject( this.SceneObject );
        }

        public void Redo()
        {
            Debug.Assert( _previousSelection.IsEquivalentTo( this.SceneModel.Selection ) );

            this.SceneObject.Position = this.DropPosition;
            this.SceneModel.SceneGraph.AddSceneObject( this.SceneObject );
            this.SceneModel.Selection.Set( this.SceneObject );
        }
    }

    #if ENGINE_OF_EVERMORE_ENABLE_UNIT_TESTS
    namespace Test
    {
        using NUnit.Framework;
        
        [TestFixture]
        public class ObjectLibraryDropCommandFixture
        {
            const String RESOURCE_NAME = "TheResourceName";

            [Test]
            public void TestCreation()
            {
                using ( var model = SceneModel.CreateNewSceneModel() )
                {
                    var dropPosition = new Vector2d( 30, 40 );
                    var sceneObject = new SceneObject();

                    var command = new ObjectLibraryDropCommand( model, sceneObject, 
                        RESOURCE_NAME, dropPosition );

                    Assert.That( command.SceneModel, Is.SameAs( model ) );
                    Assert.That( command.SceneObject, Is.SameAs( sceneObject ) );
                    Assert.That( command.ResourceName, Is.EqualTo( RESOURCE_NAME ) );
                    Assert.That( command.DropPosition.EpsilonEquals( dropPosition ) );
                }
            }

            [Test]
            public void TestExecute()
            {
                using ( var model = SceneModel.CreateNewSceneModel() )
                {
                    var sceneObject = new SceneObject();
                    var dropPosition = new Vector2d( 20, 30 );

                    var command = new ObjectLibraryDropCommand( model, sceneObject,
                        RESOURCE_NAME, dropPosition );

                    command.Execute();

                    Assert.That( model.SceneGraph.SceneObjects.Contains( sceneObject ) );
                    Assert.That( model.Selection.Count, Is.EqualTo( 1 ) );
                    Assert.That( model.Selection.Contains( sceneObject ) );
                    Assert.That( sceneObject.Position.EpsilonEquals( dropPosition ) );
                }
            }

            [Test]
            public void TestUndo()
            {
                using ( var model = SceneModel.CreateNewSceneModel() )
                {
                    model.SceneGraph.AddSceneObject( new SceneObject() );

                    model.Selection.Set( model.SceneGraph.SceneObjects.Single() );

                    var oldSelection = model.Selection.ToArray();

                    var sceneObject = new SceneObject();
                    var dropPosition = new Vector2d( 25, 530 );

                    var command = new ObjectLibraryDropCommand( model, sceneObject,
                        RESOURCE_NAME, dropPosition );

                    command.Execute();
                    command.Undo();

                    Assert.That( sceneObject.SceneGraph, Is.Null );
                    Assert.That( model.SceneGraph.SceneObjects, Has.No.Member( sceneObject ) );
                    Assert.That( model.Selection, Is.EquivalentTo( oldSelection ) );
                }
            }

            [Test]
            public void TestRedo()
            {
                using ( var model = SceneModel.CreateNewSceneModel() )
                {
                    model.SceneGraph.AddSceneObject( new SceneObject() );

                    model.Selection.Set( model.SceneGraph.SceneObjects.Single() );

                    var sceneObject = new SceneObject();
                    var dropPosition = new Vector2d( 25, 530 );

                    var command = new ObjectLibraryDropCommand( model, sceneObject,
                        RESOURCE_NAME, dropPosition );

                    command.Execute();
                    command.Undo();
                    command.Redo();

                    Assert.That( model.SceneGraph.SceneObjects.Contains( sceneObject ) );
                    Assert.That( model.Selection.Count, Is.EqualTo( 1 ) );
                    Assert.That( model.Selection.Contains( sceneObject ) );
                    Assert.That( sceneObject.Position.EpsilonEquals( dropPosition ) );
                }
            }
        }
    }
    #endif
}
